<template>
  <Panel>
    <template #title>
      <span>管理员信息</span>
    </template>

    <template #content>
      <div>
        <ul>
          <li>
            <strong>管理员ID:</strong><span>{{ users.id }}</span>
          </li>
          <li>
            <strong>账号:</strong><span>{{ users.account }}</span>
          </li>
          <li>
            <strong>用户组:</strong><span>{{ users.userGroup }}</span>
          </li>
          <li>
            <strong>创建时间:</strong><span>{{ users.ctime }}</span>
          </li>
          <li>
            <strong>管理员头像:</strong>
            <!-- <el-avatar
              shape="square"
              :size="100"
              :fit="'contain'"
              :src="users.imgUrl"
            ></el-avatar> -->
            <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:5000/users/avatar_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </li>
          <li>
            <el-button type="primary" size="mini" @click="saveAvatar"
              >保存</el-button
            >
          </li>
        </ul>
      </div>
    </template>
  </Panel>
</template>

<script>
import moment from 'moment'
import local from '../../utils/local.js'
import { editAvatar } from '../../api/users.js'

export default {
  data() {
    return {
      users: {},
      imageUrl: '',
      imgUrl: ''
    }
  },
  created() {
    this.users = JSON.parse(local.get('users'))
    this.users.ctime = moment(this.users.ctime).format('YYYY-MM-DD HH:mm:ss')
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      // 把上传成功的地址，存到了本地的data中
      this.imgUrl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const arr = ['image/jpeg', 'image/jpg', 'image/webp', 'image/gif']
      const isJPG = arr.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('头像图片不符合格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 保存头像的请求
    async saveAvatar() {
      const data = await editAvatar({
        imgUrl: this.imgUrl
      })
      if (data.code === 0) {
        location.reload()
      }
    }
  }
}
</script>

<style lang="less" scoped>
li {
  margin: 20px 0;
}

strong {
  font-weight: 700;
  margin-right: 10px;
}

/deep/.avatar-uploader .el-upload {
  margin: 20px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
